package com.sgb.kjwl.viewmodel.machine;

import android.text.TextUtils;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.model.entity.BiddingEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.MachineRepertory;
import com.sgb.kjwl.utils.AddressHelper;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.fragment.machine.AskLeaseBiddingFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AskLeaseBiddingFragmentModel extends BaseViewModel {

    private final AskLeaseBiddingFragment fragment;
    private final MachineRepertory repertory;
    private int page = 1;

    public AskLeaseBiddingFragmentModel(AskLeaseBiddingFragment fragment, MachineRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    public String getAreaNameModel() {
        return repertory.getAreaName();
    }

    public String getAreaCodeModel() {
        return repertory.getAreaCode();
    }

    public void getBiddingList(String type, String provinceNo, String cityNo, String areaId, String goodsType, String price, String sortMethod, String timeNo, boolean b) {
        HashMap map = new HashMap();
        page = b ? page + 1 : 1;
        map.put("limit", 10);
        map.put("name", "");
        map.put("page", page);
        if (TextUtils.isEmpty(type)) {
            map.put("typeValue", "1和5");
        } else {
            map.put("type", type);
        }
        if (!TextUtils.isEmpty(provinceNo))
            map.put("areacode", provinceNo);
        if (!TextUtils.isEmpty(cityNo))
            map.put("city", cityNo);
        if (!TextUtils.isEmpty(areaId))
            map.put("areaId", areaId);
        if (!TextUtils.isEmpty(price))
            map.put("price", price);
        if (!TextUtils.isEmpty(timeNo)) {
            map.put("time", timeNo);
        }
        if (!TextUtils.isEmpty(goodsType)) {
            map.put("goodsType", goodsType);
        }
        map.put("sortMethod", Integer.valueOf(sortMethod));
        DialogHelper.showProgressDialog(fragment.getActivity(), "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getBiddingList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<BiddingEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<BiddingEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showList(data.getData().getList(), b);
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }


    //    public void getDataList(String categoryType, String classNo, String typeNo,String provinceNo, String cityNo, String addrNo, String sortNo, int page) {
//
//        if ("".equals(categoryType)) {
//            LeaseAskFilterJson filterJson = new LeaseAskFilterJson();
//            filterJson.setProvinceNo(provinceNo);
//            filterJson.setCityNo(cityNo);
//            filterJson.setCurrent(page);
//            filterJson.setSortWay(sortNo);
//            if (!TextUtils.isEmpty(addrNo)) {
//                List<String> addrNos = Arrays.asList(addrNo.split(","));
//                filterJson.setAreaNos(addrNos);
//            }
//            getAskRentLeaseAll(filterJson);
//            return;
//        }
//
//        Map map = new HashMap();
//        map.put("areacode", provinceNo);
//        map.put("rentalTitle", "");
//        map.put("PageNum", page);
//        map.put("pageSize", 10);
//        map.put("categoryType", categoryType);//租赁类目(0:机械;1:设备;2:车辆;3:房产;)
//        map.put("categoryParentId", classNo);//分类ID
//        map.put("categoryChildId", typeNo);//类型ID
//        map.put("rentAddressProvinceId", provinceNo);//区域编号
//        map.put("cityId", cityNo);//市ID
//        map.put("sortWay", sortNo);//排序
//        if (!TextUtils.isEmpty(addrNo)) {
//            map.put("areaId", addrNo);//区,多选
//        }
//
//        APIManager.getInstance().getMachineAPI().getEquList(map).enqueue(new Callback<BaseEntity<ProListEntity<List<AskLeaseSheBeiEntity>>>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<ProListEntity<List<AskLeaseSheBeiEntity>>>> call, Response<BaseEntity<ProListEntity<List<AskLeaseSheBeiEntity>>>> response) {
//                if (response.body() == null || response.body().getState() == null) {
//                    return;
//                }
//
//                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    if (null != response.body().getData()) {
//                        fragment.showList(response.body().getData().getList());
//                    } else {
//                        MToast.showToast(fragment.getActivity(), "暂无数据");
//                    }
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<ProListEntity<List<AskLeaseSheBeiEntity>>>> call, Throwable t) {
//                Log.v("dd", "sdsdd");
//            }
//        });
//    }
//
//    public void getAskRentLeaseAll(LeaseAskFilterJson leaseAskFilterJson) {
//        APIManager.getInstance().getMachineAPI().getAskRentLeaseAll(leaseAskFilterJson).enqueue(new Callback<BaseEntity<ProListEntity<List<AskLeaseSheBeiEntity>>>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<ProListEntity<List<AskLeaseSheBeiEntity>>>> call, Response<BaseEntity<ProListEntity<List<AskLeaseSheBeiEntity>>>> response) {
//                if (response.body() == null || response.body().getState() == null) {
//                    return;
//                }
//
//                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    if (null != response.body().getData()) {
//                        fragment.showList(response.body().getData().getList());
//                    } else {
//                        MToast.showToast(fragment.getActivity(), "暂无数据");
//                    }
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<ProListEntity<List<AskLeaseSheBeiEntity>>>> call, Throwable t) {
//                Log.v("dd", "sdsdd");
//            }
//        });
//    }
    public void getBidSortSelect() {
        APIManager.getInstance().getPickAPI().getBidSortSelect().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setSortData(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {

            }
        });
    }

    public void getPriceData() {
        APIManager.getInstance().getPickAPI().getProjectMoney().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                if (data == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setPriceDatas(data.getData());
                }
            }
        });
    }

    public void getPickTimes() {
        APIManager.getInstance().getPickAPI().getPickTimes().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                if (data == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setTimeDatas(data.getData());
                }
            }
        });
    }

    public void getUrlSetting(String id) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("competitivePurchase").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String url = data.getData().getUrl() + "?id=" + id;
                    String lng = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
                    String lat = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "");
                    url = url + "&lng=" + lng + "&lat=" + lat;
                    WebViewX5HostActivity.start(fragment.getActivity(), url, id);
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }

    /**
     * 获取省市区地址
     */
    public void getCityData() {
        DialogHelper.showProgressDialog(fragment.getActivity(), "加载中...").setCanceledOnTouchOutside(false);
        AddressHelper.getInstance().getAllArea(new AddressHelper.AddressCallBack() {
            @Override
            public void initialData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.size() > 0) {
                    fragment.showAddressDatas(data, repertory.getAreaCode());
                }
            }
        });
    }
}
